package org.geeks.gscaffeine.func;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class CaffeineRefreshT {

  @Test
  public void sizeBased() throws Exception {
    LoadingCache<String, String> cache0 =
        Caffeine.newBuilder()
            .maximumSize(10_000)
            .refreshAfterWrite(1, TimeUnit.SECONDS)
            .build(
                key -> {
                  log.info("Refresh value");
                  return "Value of " + key;
                });

    cache0.put("Geek-S", "Hello, world");

    log.info("Value of Geek-S is {}", cache0.getIfPresent("Geek-S"));

    Thread.sleep(1000);

    log.info("Value of Geek-S is {}", cache0.getIfPresent("Geek-S"));
  }
}
